package com.api.component;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.api.common.Common;
import com.api.mapper.BaseDataMapper;
import com.api.model.entity.BaseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 基础数据组件
 * 
 * @author user
 *
 */
@Component
public class BaseDataComponent {
	@Autowired
	private BaseDataMapper baseDataMapper;

	/**
	 * 通过类型获取数据，null返回所有数据
	 * 
	 * @param type
	 * @return
	 */
	public List<BaseData> getBaseDataByType(String type) {
		LambdaQueryWrapper<BaseData> queryWrapper = new LambdaQueryWrapper<BaseData>();
		queryWrapper.eq(BaseData::getStatus, true).orderByAsc(BaseData::getType);
		if (StringUtils.isNoneBlank(type)) {
			queryWrapper.eq(BaseData::getType, type);
		}
		return baseDataMapper.selectList(queryWrapper);
	}

	/**
	 * 通过类型和固定代码获取唯一基础数据
	 * 
	 * @param type
	 * @param code
	 * @return
	 */
	public BaseData getBaseDataByType(String type, String code) {
		LambdaQueryWrapper<BaseData> queryWrapper = new LambdaQueryWrapper<BaseData>();
		queryWrapper.eq(BaseData::getType, type).eq(BaseData::getCode, code).eq(BaseData::getStatus, true);
		return baseDataMapper.selectOne(queryWrapper);
	}
}
